#include "Headers\UI\typeofroomaddeditdialog.h"
#include "ui_typeofroomaddeditdialog.h"

#include <QTextCodec>

TypeOfRoomAddEditDialog::TypeOfRoomAddEditDialog(MyUtils::FormStatus formStatus, TypeOfRoom *typeOfRoom, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TypeOfRoomAddEditDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::Dialog |
                         Qt::WindowMinimizeButtonHint |
                         Qt::WindowMaximizeButtonHint |
                         Qt::WindowStaysOnTopHint);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    this->formStatus = formStatus;
    this->typeOFRoom = typeOfRoom;

    if (formStatus == MyUtils::Editing)
    {
        ui->createButton->setText(trUtf8("Изменить"));
        if (typeOfRoom != NULL)
        {
            ui->NameLineEdit->setText(typeOfRoom->getName());
            //ui->CountLineEdit->setText(QString::number(typeOfRoom->getCount()));
            ui->countSpinBox->setValue(typeOfRoom->getCount());
            ui->costSpinBox->setValue(typeOfRoom->getCost());
        }
    }
}

TypeOfRoomAddEditDialog::~TypeOfRoomAddEditDialog()
{
    delete ui;
}

/**
  Отмена
  */
void TypeOfRoomAddEditDialog::on_cancelButton_clicked()
{
    this->close();
}

/**
  Создать
  */
void TypeOfRoomAddEditDialog::on_createButton_clicked()
{
    TypeOfRoomDataAccessor dataAccessor;
    TypeOfRoom typeOfRoom;
    typeOfRoom.setName(ui->NameLineEdit->text());
    //typeOfRoom.setCount(ui->CountLineEdit->text().toInt());
    typeOfRoom.setCount(ui->countSpinBox->value());
    typeOfRoom.setCost(ui->costSpinBox->value());
    if (formStatus == MyUtils::Adding)
    {
        dataAccessor.setTypeOfRoom(typeOfRoom);
    }
    else if (formStatus == MyUtils::Editing)
    {
        typeOfRoom.setId(this->typeOFRoom->getId());
        dataAccessor.editTypeOfRoom(typeOfRoom);
    }
    this->close();
}
